﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Microsoft.SqlServer.Management.Smo;

namespace CLFramework.MSSqlExport
{
    public class ScriptConfig
    {
        public ScriptConfig() { }

        public ScriptConfig(XmlNode node)
        {
            this.Schema = node.GetStringAttribute("schema", "");
            this.Filename = node.GetStringAttribute("filename", "");
            this.ScriptDrops = node.GetBoolAttribute("scriptDrops", false);
            this.SchemaQualify = node.GetBoolAttribute("schemaQualify", true);
            this.ServerVersion = node.GetEnumAttribute<ServerVersion>("serverVersion", MSSqlExport.ServerVersion.SqlServer2008);
            this.PublishType = node.GetEnumAttribute<PublishType>("publishType", MSSqlExport.PublishType.SchemaOnly);
            this.objectOptions = node.GetEnumAttribute<ObjectOptions>("objectOptions", ObjectOptions.T | ObjectOptions.V | ObjectOptions.F | ObjectOptions.P);
        }

        /// <summary>
        /// 根据Schema生成对应脚本
        /// </summary>
        public string Schema { get; set; }

        /// <summary>
        /// 保存的路径
        /// </summary>
        public string Filename { get; set; }

        /// <summary>
        /// 是否生成Drop脚本
        /// </summary>
        public bool ScriptDrops { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public bool SchemaQualify { get; set; }

        /// <summary>
        /// Sql Server版本
        /// </summary>
        public ServerVersion ServerVersion { get; set; }

        public SqlServerVersion SqlServerVersion
        {
            get {
                if (ServerVersion == MSSqlExport.ServerVersion.SqlServer2000)
                    return Microsoft.SqlServer.Management.Smo.SqlServerVersion.Version80;
                else if (ServerVersion == MSSqlExport.ServerVersion.SqlServer2005)
                    return Microsoft.SqlServer.Management.Smo.SqlServerVersion.Version90;
                else if (ServerVersion == MSSqlExport.ServerVersion.SqlServer2008)
                    return Microsoft.SqlServer.Management.Smo.SqlServerVersion.Version100;
                return Microsoft.SqlServer.Management.Smo.SqlServerVersion.Version80;
            }
        }

        /// <summary>
        /// 生成脚本类型
        /// </summary>
        public PublishType PublishType { get; set; }

        /// <summary>
        /// 导出对象类型
        /// </summary>
        public ObjectOptions objectOptions { get; set; }

        /// <summary>
        /// 是否只输出结构,不输出数据
        /// </summary>
        public bool SchemaOnly { get; set; }
    }

    public enum ServerVersion
    {
        SqlServer2000,
        SqlServer2005,
        SqlServer2008,
    }

    public enum PublishType
    {
        SchemaOnly,
        DataOnly,
        SchemaAndData,
    }

    [Flags]
    public enum ObjectOptions : uint
    {
        T = 1,
        V = 2,
        F = 4,
        P = 8
    }
}
